package com.jzg.jcptadmin.presenter;

import com.jzg.jcptadmin.base.BaseObject;
import com.jzg.jcptadmin.base.BasePresenter;
import com.jzg.jcptadmin.constant.Constant;
import com.jzg.jcptadmin.data.DataManager;
import com.jzg.jcptadmin.data.vo.TouXiangInfo;
import com.jzg.jcptadmin.viewinterface.UserInfoInterface;

import java.util.Map;

import rx.Observer;
import rx.Subscription;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * Created by jzg on 2016/2/29.
 */
public class UserInfoPresenter extends BasePresenter<UserInfoInterface> {
    private Subscription mSubscription;
    private UserInfoInterface view;
    private final DataManager mDataManager;

    public UserInfoPresenter(DataManager mDataManager) {
        this.mDataManager = mDataManager;
    }

    @Override
    public void attachView(UserInfoInterface mvpView) {
        super.attachView(mvpView);
        this.view = mvpView;
    }

    @Override
    public void detachView() {
        super.detachView();
        if (mSubscription != null)
            mSubscription.unsubscribe();
    }

    public void updateNickName() {
        Map<String, String> params = view.getNickNameParams();
        if (!checkSubmitInfo(params))
            return;
        mSubscription = mDataManager.updateNickName(params).observeOn(AndroidSchedulers.mainThread())
                .subscribeOn(Schedulers.newThread()).subscribe(new Observer<BaseObject>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {
                        if (e != null&& view!=null) {
                            view.showError(Constant.errr_net);
                        }
                    }

                    @Override
                    public void onNext(BaseObject baseObject) {
                        if (baseObject.getStatus() == Constant.SUCCESS) {
                            view.updateNickName(baseObject);
                        } else {
                            view.showError(baseObject.getMsg());

                        }
                    }
                });
    }

    public void updateImage(String userid,String path) {
        mSubscription = mDataManager.updateImage(userid,path).observeOn(AndroidSchedulers.mainThread())
                .subscribeOn(Schedulers.newThread()).subscribe(new Observer<TouXiangInfo>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {
                        if (e != null) {
                            view.showError(Constant.errr_net);
                        }
                    }

                    @Override
                    public void onNext(TouXiangInfo info) {
                        if (info.getStatus() == Constant.SUCCESS) {
                            view.updateImg(info);
                        } else {
                            view.showError(info.getMsg());

                        }
                    }
                });
    }
}
